$(function () {
    var form = layui.form;
    var layer = layui.layer;
    var laypage = layui.laypage;
    // 点击“文字链接”的链接
    $(".layui-this-right").on("click", function () {
        $("#right").show();
        $("#lift").hide();
    });
    // 点击“图片链接”的链接
    $(".layui-this").on("click", function () {
        $("#right").hide();
        $("#lift").show();
    });
    // // 新增
    var indexAdd = null;
    $("#btnAddCate").on("click", function () {
        indexAdd = layer.open({
            type: 1,
            area: ["600px", "400px"],
            title: "添加友情链接",
            content: $("#dialog-add").html(),

            // success: function () {
            //     form.render();
            // },
        });
    });
    $('body').on('submit', '#addCateForm', function (e) {
        e.preventDefault();
        var q = {
            des: $('[name=des]').val(),
            img: '',
            title: $('[name=title]').val(),
            type: '1',
            url: $('[name=url]').val(),
        };
        console.log('--------------------------');
        $.ajax({
            method: 'post',
            url: "http://124.223.14.236:8060/admin/ydd_link/add",
            data: q,
            headers: {
                //"x-token": localStorage.getItem("x-token") || "",
                'x-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ4MzE0Njh9.XWO9VB_w2J3vBIPo6ga62_xqqCVyR1R0fhHSvrHsh6A'
            },
            success: function (res) {
                console.log(res);
                initTable()
            }
        })
    })
    var q = {
        type: 1,
        page: 1,
        limit: 10,
    };
    // 获取友情链接列表数据的方法
    initTable();
    initCate();
    gettable();
    function initTable() {
        $.ajax({
            method: "get",
            url: "http://124.223.14.236:8060/admin/ydd_link/list",
            data: q,
            headers: {
                //"x-token": localStorage.getItem("x-token") || "",
                'x-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ4MzE0Njh9.XWO9VB_w2J3vBIPo6ga62_xqqCVyR1R0fhHSvrHsh6A'
            },
            success: function (res) {
                console.log(res);
                if (res.errno !== 0) {
                    return layer.msg(res.errmsg);
                }
                var htmlStr = template("tpl-table", res.data);
                $("tbody").html(htmlStr);
                // 调用渲染分页的方法
                renderPage(res.data.count);
                renderRight(res.data.count);
            },
        });
    }


    // 编辑修改
    var layerIndex = null;
    $("tbody").on("click", "#amend", function () {
        layerIndex = layer.open({
            type: 1,
            area: ["600px", "400px"],
            title: "添加文章分类",
            content: $("#dialog-edit").html(),
            success: function () {
                form.render();
            },
        });

        var cate_id = $(this).attr("data-id");
        $.ajax({
            method: "get",
            url: "http://124.223.14.236:8060/admin/ydd_link/rowInfo?id=" + cate_id,
            data: $(this).serialize(),
            headers: {
                //"x-token": localStorage.getItem("x-token") || "",
                'x-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ4MzE0Njh9.XWO9VB_w2J3vBIPo6ga62_xqqCVyR1R0fhHSvrHsh6A'
            },
            success: function (res) {
                form.val("form-edit", res.data);
            },
        });
    });

    // 提交编辑
    $("body").on("submit", "#form-edit", function (e) {
        e.preventDefault();
        $.ajax({
            url: "http://124.223.14.236:8060/admin/ydd_link/update",
            method: "POST",
            data: $(this).serialize(),
            headers: {
                //"x-token": localStorage.getItem("x-token") || "",
                'x-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ4MzE0Njh9.XWO9VB_w2J3vBIPo6ga62_xqqCVyR1R0fhHSvrHsh6A'
            },
            success: function (res) {
                // if (res.status !== 0) {
                //  return layer.msg(res.message);
                // }
                // layer.msg("更新成功");
                initTable();
                layer.close(layerIndex);
            },
        });
    });


    // 初始化文章分类的方法
    function initCate() {
        $.ajax({
            method: "GET",
            url: "http://124.223.14.236:8060/admin/ydd_link/rowInfo",
            data: {},
            headers: {
                //"x-token": localStorage.getItem("x-token") || "",
                'x-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ4MzE0Njh9.XWO9VB_w2J3vBIPo6ga62_xqqCVyR1R0fhHSvrHsh6A'
            },
            success: function (res) {
                if (res.errno !== 0) {
                    return layer.msg(res.errmsg);
                }
                // 调用模板引擎渲染分类的可选项
                var htmlStr = template("tpl-cate", res.data);
                $("[name=cate_id]").html(htmlStr);
                // 通过 layui 重新渲染表单区域的UI结构
                form.render();
            },
        });
    }
    // 图片
    var p = {
        type: 2,
        page: 1,
        limit: 10,
    };

    function gettable() {
        $.ajax({
            method: "get",
            url: "http://124.223.14.236:8060/admin/ydd_link/list",
            data: p,
            headers: {
                //"x-token": localStorage.getItem("x-token") || "",
                'x-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ4MzE0Njh9.XWO9VB_w2J3vBIPo6ga62_xqqCVyR1R0fhHSvrHsh6A'
            },
            success: function (res) {
                console.log(res);
                if (res.errno !== 0) {
                    return layer.msg(res.errmsg);
                }
                var htmlStr1 = template("tpl-img", res.data);
                $(".img-tpl").html(htmlStr1);
            },
        });
    }
    // 通过代理的形式，为删除按钮绑定点击事件处理函数
    $("tbody").on("click", ".btn-delete", function () {
        // 获取删除按钮的个数
        var len = $(".btn-delete").length;
        console.log(len);
        // 获取到文章的 id
        var id = $(this).attr("data-id");
        // 询问用户是否要删除数据
        layer.confirm("确认删除?", { icon: 3, title: "提示" }, function (index) {
            $.ajax({
                method: "GET",
                url: "http://124.223.14.236:8060/admin/ydd_link/del?id=" + id,
                headers: {
                    //"x-token": localStorage.getItem("x-token") || "",
                    'x-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6eyJpZCI6IjEiLCJsb2dpbl9uYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6Ijk2ZTc5MjE4OTY1ZWI3MmM5MmE1NDlkZDVhMzMwMTEyIiwibmFtZSI6IlRoaW5rSlPkuIDlk6UiLCJlbWFpbCI6Imhld3UxMTIyQDE2My5jb20iLCJwaG9uZSI6IjE3MzQyMDY1OTA5IiwibG9naW5faXAiOiI6OjEiLCJsb2dpbl9kYXRlIjoiMjAxOC0wMS0wOCAxNjoyMjo1MSIsImNyZWF0ZV9kYXRlIjoiMjAxNy0xMi0xNCAxMTo1Mjo0NiIsInVwZGF0ZV9kYXRlIjoiMjAyMS0wOS0wOCAxOTo0MzowMyIsImRlbF9mbGFnIjoiMCIsInJvbGVfaWQiOlsiMWM1NGUwMDNjMWZjNGRjZDliMDg3ZWY4ZDQ4YWJhYzMiXSwic3RhdHVzIjoxLCJkZWZhdWx0X2RhdGEiOiIxIiwicm9sZW5hbWUiOlsi6LaF57qn566h55CG5ZGYIl19LCJpYXQiOjE2NTQ4MzE0Njh9.XWO9VB_w2J3vBIPo6ga62_xqqCVyR1R0fhHSvrHsh6A'
                },
                success: function (res) {
                    if (res.errno !== 0) {
                        return layer.msg(res.errmsg);
                    }
                    layer.msg("删除成功！");
                    // 当数据删除完成后，需要判断当前这一页中，是否还有剩余的数据
                    // 如果没有剩余的数据了,则让页码值 -1 之后,
                    // 再重新调用 initTable 方法
                    // 4
                    if (len === 1) {
                        // 如果 len 的值等于1，证明删除完毕之后，页面上就没有任何数据了
                        // 页码值最小必须是 1
                        q.page = q.page === 1 ? 1 : q.page - 1;
                    }
                    initTable();
                },
            });
            layer.close(index);
        });
    });
    // 定义渲染分页的方法
    // 文字链接
    function renderPage(total) {
        // 调用 laypage.render() 方法来渲染分页的结构
        laypage.render({
            elem: "pageBox", // 分页容器的 Id
            count: total, // 总数据条数
            limit: q.limit, // 每页显示几条数据
            curr: q.page, // 设置默认被选中的分页
            layout: ["count", "limit", "prev", "page", "next", "skip"],
            limits: [2, 3, 5, 10],
            // 分页发生切换的时候，触发 jump 回调
            // 触发 jump 回调的方式有两种：
            // 1. 点击页码的时候，会触发 jump 回调
            // 2. 只要调用了 laypage.render() 方法，就会触发 jump 回调
            jump: function (obj, first) {
                // 可以通过 first 的值，来判断是通过哪种方式，触发的 jump 回调
                // 如果 first 的值为 true，证明是方式2触发的
                // 否则就是方式1触发的
                console.log(first);
                console.log(obj.curr);
                // 把最新的页码值，赋值到 q 这个查询参数对象中
                p.page = obj.curr;
                // 把最新的条目数，赋值到 q 这个查询参数对象的 pagesize 属性中
                p.limit = obj.limit;
                // 根据最新的 q 获取对应的数据列表，并渲染表格
                // initTable()
                if (!first) {
                    initTable();
                }
            },
        });
    }

    // 图片
    function renderRight(total) {
        // 调用 laypage.render() 方法来渲染分页的结构
        laypage.render({
            elem: "pageBox", // 分页容器的 Id
            count: total, // 总数据条数
            limit: p.limit, // 每页显示几条数据
            curr: p.page, // 设置默认被选中的分页
            layout: ["count", "limit", "prev", "page", "next", "skip"],
            limits: [2, 3, 5, 10],
            // 分页发生切换的时候，触发 jump 回调
            // 触发 jump 回调的方式有两种：
            // 1. 点击页码的时候，会触发 jump 回调
            // 2. 只要调用了 laypage.render() 方法，就会触发 jump 回调
            jump: function (obj, first) {
                // 可以通过 first 的值，来判断是通过哪种方式，触发的 jump 回调
                // 如果 first 的值为 true，证明是方式2触发的
                // 否则就是方式1触发的
                console.log(first);
                console.log(obj.curr);
                // 把最新的页码值，赋值到 p 这个查询参数对象中
                q.page = obj.curr;
                // 把最新的条目数，赋值到 p这个查询参数对象的 pagesize 属性中
                q.limit = obj.limit;
                // 根据最新的 q 获取对应的数据列表，并渲染表格
                // initTable()
                if (!first) {
                    initTable();
                }
            },
        });
    }
});
